"""
post请求，数据不在url路径里面，必须进行编码
"""
import urllib.request
import urllib.parse
import json
import urllib.error

# 请求体数据 data editplus ctrl+H -> 正则表达式替换
data = {
    'aiType': '2',
    'from': 'en',
    'select': 'all',
    'sentence': '[{"id":"DBP44bQZWB","paraIdx":0,"src":"spid\n","dst":"spid\n","metadata":"","metaData":[]}]',
    'to': 'zh',
    'type': '1',
    'typeID': 'DPD0ZqB3kP',
}

data = urllib.parse.urlencode(data)  # 字符串类型，但是post请求体发送需要字节型数据
data = data.encode("utf-8")  # post 请求的字节型数据
# 访问路径
url = "https://fanyi.baidu.com/ai/smartSug"

# 定制headers--->反爬
headers = {
    'Accept': '*/*',
    # # 'Accept-Encoding': 'gzip, deflate, br', # 一定要注释掉这句话
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    # 'Content-Length': '237',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': 'BIDUPSID=8AC47C91EB24CEF54D60954251023E56;PSTM=1662460286;REALTIME_TRANS_SWITCH=1;FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1;SOUND_SPD_SWITCH=1;SOUND_PREFER_SWITCH=1; BAIDUID=1C81DFCBB13256AD62B7F7C55C088BF0:FG=1;Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1710755946;H_WISE_SIDS_BFESS=60236_60298_60326;BDUSS=l1VjEyUm1CMjkyODFHVjktak9SR35rVVQ3RUhTVmxVMUR2clh4ODhsOHo2WTltRVFBQUFBJCQAAAAAAQAAAAEAAABFqE5lVm9rdGEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADNcaGYzXGhmNG;BDUSS_BFESS=l1VjEyUm1CMjkyODFHVjktak9SR35rVVQ3RUhTVmxVMUR2clh4ODhsOHo2WTltRVFBQUFBJCQAAAAAAQAAAAEAAABFqE5lVm9rdGEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADNcaGYzXGhmNG;BAIDUID_BFESS=1C81DFCBB13256AD62B7F7C55C088BF0:FG=1;ZFY=QA1Cl0N7KouaHe:A8lZEMeUihNzxy5WzBRcUKjovdk5Q:C;H_PS_PSSID=60236_60326_60334_60297_60345; H_WISE_SIDS=60236_60326_60334_60297_60345;BDORZ=FFFB88E999055A3F8A630C64834BD6D0;BA_HECTOR=01a0848l8ha58l0la121010h2ompl11j6o2v51v;ab_sr=1.0.1_NWMyNGFiNTFkM2FiNzA0N2ViNWRiZmNhNGQ4M2JkMWQ3NjBmYTU3Nzg3ZGQ3YTk5OTAyZDE3NDYzNzk0ZGFmOWU1MTJlMmQwYWU5YTcxZTQ3NWQzMWQ5OTEzZTlhNjhlNDkzMmQyYTFjNjRkZGY3NzYxY2EyNWVlMzY4OWE4YjNmZDcxMDlmODkwZDEyOGIzY2U5OTdiNWU5NzFkMWNiNA==;RT="z=1&dm=baidu.com&si=2f056dce-669f-4af1-b8cc-16e2e34928c9&ss=lxejnd6e&sl=1&tt=2sw&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3l6"',
    'Host': 'fanyi.baidu.com',
    'Origin': 'https://fanyi.baidu.com',
    'Referer': 'https://fanyi.baidu.com/mtpe-individual/multimodal?query=spid&lang=en2zh&ext_channel=Aldtype',
    'sec-ch-ua': '"Chromium";v="9", "Not?A_Brand";v="8"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': 'Windows',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105',
}

# 请求体放在UA的初始化中
my_request = urllib.request.Request(url=url, headers=headers, data=data)
try:
    response = urllib.request.urlopen(my_request)
    print(response.getcode())
    content = response.read().decode("utf-8")
    print(content)  # 响应的类型--->json形式的str
    dict = json.loads(content)
    print(dict)
except urllib.error.HttpError as he:
    print(he)
except urllib.error.URLError as ue:
    print(ue)
except Exception as e:
    print(e)
